﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class Dm_QuyCachBLL
    {
        public Dm_QuyCachBLL()
        {
        }

        public Dm_QuyCach AddNewDmQuyCach(Dm_QuyCach objectDmQuyCach)
        {
            Dm_QuyCach dmQCTemple = new Dm_QuyCach();
            int error = -1;
            try
            {
                dmQCTemple = objectDmQuyCach;
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    entities.Dm_QuyCach.Add(dmQCTemple);
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmQCTemple = null;
                    }
                }
            }
            catch
            {
            }
            return dmQCTemple;
        }

        public bool UpdateDmQuyCach(Dm_QuyCach objectDmQuyCach)
        {
            Dm_QuyCach dmQCTemple = new Dm_QuyCach();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmQCTemple = entities.Dm_QuyCach.SingleOrDefault(c => c.MaQuyCach == objectDmQuyCach.MaQuyCach);
                    dmQCTemple.TenQuyCach = objectDmQuyCach.TenQuyCach;
                    dmQCTemple.KichHoat = objectDmQuyCach.KichHoat;
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool DeleteDmQuyCach(Dm_QuyCach objectDmQuyCach)
        {
            Dm_QuyCach dmQCTemple = new Dm_QuyCach();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmQCTemple = entities.Dm_QuyCach.SingleOrDefault(c => c.MaQuyCach == objectDmQuyCach.MaQuyCach);
                    entities.Dm_QuyCach.Remove(dmQCTemple);
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public List<Dm_QuyCach> ListDmQuyCach()
        {
            List<Dm_QuyCach> dmQCTemple = new List<Dm_QuyCach>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmQCTemple = (from c in entities.Dm_QuyCach select c).ToList();
                }
            }
            catch
            {
            }
            return dmQCTemple;
        }

        public List<Dm_QuyCach> FindListDmQuyCach(string maQC, string tenQC)
        {
            List<Dm_QuyCach> dmQCTemple = new List<Dm_QuyCach>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (!string.IsNullOrEmpty(maQC))
                    {
                        dmQCTemple = (from c in entities.Dm_QuyCach
                                       where c.MaQuyCach.Contains(maQC)
                                       select c).ToList();
                    }
                    else if (!string.IsNullOrEmpty(tenQC))
                    {
                        dmQCTemple = (from c in entities.Dm_QuyCach
                                       where c.MaQuyCach.Contains(tenQC)
                                       select c).ToList();
                    }
                    else
                    {
                        dmQCTemple = (from c in entities.Dm_QuyCach
                                       where c.MaQuyCach.Contains(maQC)
                                             && c.MaQuyCach.Contains(tenQC)
                                       select c).ToList();
                    }
                }
            }
            catch
            {
            }
            return dmQCTemple;
        }

        public bool FindTrungMaDmQuyCach(string maQC)
        {
            Dm_QuyCach dmQCTemple = new Dm_QuyCach();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmQCTemple = entities.Dm_QuyCach.SingleOrDefault(c => c.MaQuyCach == maQC);
                    if (dmQCTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }
    }
}
